#routine for generating latex table output
#n is the number of models, currently limited to 4

ltable <- function(n, mod1, mod2=c(), mod3=c(), mod4=c(), mod5=c(), varnames=c()){    
    mrkup <- c("{", "\\textit{","\\textbf{")
    sig <- function(mod){
        c1 <- abs(mod$par/mod$se)>=1.959964
        c2 <- abs(mod$par/mod$se)>= 1.644854
        sl <- rep(1, length(mod$par))  #significance level switch
        sl[c2] <- 2
        sl[c1] <- 3
        return(sl)
    }

    if (n==1){
        k <- length(mod1$par)
        sig1 <- sig(mod1)
        for (i in 1:k){
            cat(varnames[i], "&")
            cat(paste(mrkup[sig1[i]],signif(mod1$par[i],3),"} & (",signif(mod1$se[i],3),")", sep=""), "\\\\ \n")
        }
    }
    if (n==2){
        k <- max(length(mod1$par), length(mod2$par))
        sig1 <- sig(mod1)
        sig2 <- sig(mod2)
        for (i in 1:k){
            cat(varnames[i], "&")
            cat(paste(mrkup[sig1[i]],signif(mod1$par[i],3),"} & (",signif(mod1$se[i],3),") &", sep=""))
            cat(paste(mrkup[sig2[i]],signif(mod2$par[i],3),"} & (",signif(mod2$se[i],3),")", sep=""), "\\\\ \n")
        }
    }
    if (n==3){
        k <- max(length(mod1$par), length(mod2$par), length(mod3$par))
        sig1 <- sig(mod1)
        sig2 <- sig(mod2)
        sig3 <- sig(mod3)
        for (i in 1:k){
            cat(varnames[i], "&")
            cat(paste(mrkup[sig1[i]],signif(mod1$par[i],3),"} & (",signif(mod1$se[i],3),") &", sep=""))
            cat(paste(mrkup[sig2[i]],signif(mod2$par[i],3),"} & (",signif(mod2$se[i],3),") &", sep=""))
            cat(paste(mrkup[sig3[i]],signif(mod3$par[i],3),"} & (",signif(mod3$se[i],3),")", sep=""), "\\\\ \n")
        }
    }    
    if (n==4){
        k <- max(length(mod1$par), length(mod2$par), length(mod3$par), length(mod4$par))
        sig1 <- sig(mod1)
        sig2 <- sig(mod2)
        sig3 <- sig(mod3)
        sig4 <- sig(mod4)
        for (i in 1:k){
            cat(varnames[i], "&")
            cat(paste(mrkup[sig1[i]],signif(mod1$par[i],3),"} & (",signif(mod1$se[i],3),") &", sep=""))
            cat(paste(mrkup[sig2[i]],signif(mod2$par[i],3),"} & (",signif(mod2$se[i],3),") &", sep=""))
            cat(paste(mrkup[sig3[i]],signif(mod3$par[i],3),"} & (",signif(mod3$se[i],3),") &", sep=""))
            cat(paste(mrkup[sig4[i]],signif(mod4$par[i],3),"} & (",signif(mod4$se[i],3),")", sep=""), "\\\\ \n")
        }
    }
    if (n==5){
        k <- max(length(mod1$par), length(mod2$par), length(mod3$par), length(mod4$par), length(mod5$par))
        sig1 <- sig(mod1)
        sig2 <- sig(mod2)
        sig3 <- sig(mod3)
        sig4 <- sig(mod4)
        sig5 <- sig(mod5)
        for (i in 1:k){
            cat(varnames[i], "&")
            cat(paste(mrkup[sig1[i]],signif(mod1$par[i],3),"} & (",signif(mod1$se[i],3),") &", sep=""))
            cat(paste(mrkup[sig2[i]],signif(mod2$par[i],3),"} & (",signif(mod2$se[i],3),") &", sep=""))
            cat(paste(mrkup[sig3[i]],signif(mod3$par[i],3),"} & (",signif(mod3$se[i],3),") &", sep=""))
            cat(paste(mrkup[sig4[i]],signif(mod4$par[i],3),"} & (",signif(mod4$se[i],3),") &", sep=""))
            cat(paste(mrkup[sig5[i]],signif(mod5$par[i],3),"} & (",signif(mod5$se[i],3),")", sep=""), "\\\\ \n")
        }
    }
}
   
pvals <- function(n,  mod1, mod2=c(), mod3=c(), mod4=c(), varnames){    
    if (n>1) {
        cat("# of models not implemented yet \n\n")
        break
    }
    mod <- mod1
    parameter<-as.matrix(mod$par)
    vacov<-as.matrix(solve(-1*mod1$hessian, tol=1e-24))
    stderror<-as.matrix(sqrt(diag(vacov)))
    zstat<-as.matrix(parameter / stderror)
    pvalues <- round(2*pnorm(abs(zstat),lower.tail=FALSE),4)
    
     
    cat("results in order:", varnames,"\n")
    cat("parameter estimates: ", mod$par, "\n")
    cat("standard error: ", stderror, "\n")
    cat("z-statistics: ", zstat, "\n")
    cat("p-values: ", pvalues, "\n")
}


#p values instead of s.e.
ltable.p <- function(n, mod1, mod2=c(), mod3=c(), mod4=c(), mod5=c(), varnames=c()){    
    mrkup <- c("{", "\\textit{","\\textbf{")
    sig <- function(mod){
        c1 <- abs(mod$par/mod$se)>=1.959964
        c2 <- abs(mod$par/mod$se)>= 1.644854
        sl <- rep(1, length(mod$par))  #significance level switch
        sl[c2] <- 2
        sl[c1] <- 3
        return(sl)
    }

    if (n==1){
        k <- length(mod1$par)
        sig1 <- sig(mod1)
        ps1<- getp(mod1)
        for (i in 1:k){
            cat(varnames[i], "&")
            cat(paste(mrkup[sig1[i]],signif(mod1$par[i],3),"} & ",round(ps1[i],3),sep=""), "\\\\ \n")
        }
    }
    if (n==2){
        k <- max(length(mod1$par), length(mod2$par))
        sig1 <- sig(mod1)
        sig2 <- sig(mod2)
        ps1<- getp(mod1)
        ps2<- getp(mod2)
        for (i in 1:k){
            cat(varnames[i], "&")
            cat(paste(mrkup[sig1[i]],signif(mod1$par[i],3),"} & ",round(ps1[i],3)," &", sep=""))
            cat(paste(mrkup[sig2[i]],signif(mod2$par[i],3),"} & ",round(ps2[i],3),sep=""), "\\\\ \n")
        }
    }
    if (n==3){
        k <- max(length(mod1$par), length(mod2$par), length(mod3$par))
        sig1 <- sig(mod1)
        sig2 <- sig(mod2)
        sig3 <- sig(mod3)
        ps1<- getp(mod1)
        ps2<- getp(mod2)
        ps3<- getp(mod3)
        for (i in 1:k){
            cat(varnames[i], "&")
            cat(paste(mrkup[sig1[i]],signif(mod1$par[i],3),"} & ",round(ps1[i],3)," &", sep=""))
            cat(paste(mrkup[sig2[i]],signif(mod2$par[i],3),"} & ",round(ps2[i],3)," &", sep=""))
            cat(paste(mrkup[sig3[i]],signif(mod3$par[i],3),"} & ",round(ps3[i],3),sep=""), "\\\\ \n")
        }
    }    
    if (n==4){
        k <- max(length(mod1$par), length(mod2$par), length(mod3$par), length(mod4$par))
        sig1 <- sig(mod1)
        sig2 <- sig(mod2)
        sig3 <- sig(mod3)
        sig4 <- sig(mod4)
        ps1<- getp(mod1)
        ps2<- getp(mod2)
        ps3<- getp(mod3)
        ps4<- getp(mod4)
        for (i in 1:k){
            cat(varnames[i], "&")
            cat(paste(mrkup[sig1[i]],signif(mod1$par[i],3),"} & ",round(ps1[i],3)," &", sep=""))
            cat(paste(mrkup[sig2[i]],signif(mod2$par[i],3),"} & ",round(ps2[i],3)," &", sep=""))
            cat(paste(mrkup[sig3[i]],signif(mod3$par[i],3),"} & ",round(ps3[i],3)," &", sep=""))
            cat(paste(mrkup[sig4[i]],signif(mod4$par[i],3),"} & ",round(ps4[i],3),sep=""), "\\\\ \n")
        }
    }
    if (n==5){
        k <- max(length(mod1$par), length(mod2$par), length(mod3$par), length(mod4$par), length(mod5$par))
        sig1 <- sig(mod1)
        sig2 <- sig(mod2)
        sig3 <- sig(mod3)
        sig4 <- sig(mod4)
        sig5 <- sig(mod5)
        ps1<- getp(mod1)
        ps2<- getp(mod2)
        ps3<- getp(mod3)
        ps4<- getp(mod4)
        ps5 <- getp(mod5)
        for (i in 1:k){
            cat(varnames[i], "&")
            cat(paste(mrkup[sig1[i]],signif(mod1$par[i],3),"} & ",round(ps1[i],3)," &", sep=""))
            cat(paste(mrkup[sig2[i]],signif(mod2$par[i],3),"} & ",round(ps2[i],3)," &", sep=""))
            cat(paste(mrkup[sig3[i]],signif(mod3$par[i],3),"} & ",round(ps3[i],3)," &", sep=""))
            cat(paste(mrkup[sig4[i]],signif(mod4$par[i],3),"} & ",round(ps4[i],3)," &", sep=""))
            cat(paste(mrkup[sig5[i]],signif(mod5$par[i],3),"} & ",round(ps5[i],3), sep=""), "\\\\ \n")
        }
    }
}
   

#coefficient and s.e.
ltable.st <- function(n, mod1, mod2=c(), mod3=c(), mod4=c(), mod5=c(), varnames=c()){    
    mrkup <- c("{", "\\textit{","\\textbf{")
    sig <- function(mod){
        c1 <- abs(mod$par/mod$se)>=1.959964
        c2 <- abs(mod$par/mod$se)>= 1.644854
        sl <- rep(1, length(mod$par))  #significance level switch
        sl[c2] <- 2
        sl[c1] <- 3
        return(sl)
    }

    if (n==1){
        k <- length(mod1$par)
        sig1 <- sig(mod1)
        for (i in 1:k){
            cat(varnames[i], "&")
            cat(paste(mrkup[sig1[i]],signif(mod1$par[i],3),"} \\\\ \n", sep =""))
            cat(paste(" & \\footnotesize{(",signif(mod1$se[i],3),")} \\\\ \n", sep=""))
        }
    }
    if (n==2){
        k <- max(length(mod1$par), length(mod2$par))
        sig1 <- sig(mod1)
        sig2 <- sig(mod2)
        for (i in 1:k){
            cat(varnames[i], "&")
            cat(paste(mrkup[sig1[i]],signif(mod1$par[i],3),"} & ",mrkup[sig2[i]],signif(mod2$par[i],3),"} \\\\ \n", sep=""))
            cat(paste("& \\footnotesize{(",signif(mod1$se[i],3),")} & \\footnotesize{(",signif(mod2$se[i],3),")} \\\\ \n",sep=""))
        }
    }
    if (n==3){
        k <- max(length(mod1$par), length(mod2$par), length(mod3$par))
        sig1 <- sig(mod1)
        sig2 <- sig(mod2)
        sig3 <- sig(mod3)
        for (i in 1:k){
            cat(varnames[i], "&")
            cat(paste(mrkup[sig1[i]],signif(mod1$par[i],3),"} & (",signif(mod1$se[i],3),") &", sep=""))
            cat(paste(mrkup[sig2[i]],signif(mod2$par[i],3),"} & (",signif(mod2$se[i],3),") &", sep=""))
            cat(paste(mrkup[sig3[i]],signif(mod3$par[i],3),"} & (",signif(mod3$se[i],3),")", sep=""), "\\\\ \n")
        }
    }    
    if (n==4){
        k <- max(length(mod1$par), length(mod2$par), length(mod3$par), length(mod4$par))
        sig1 <- sig(mod1)
        sig2 <- sig(mod2)
        sig3 <- sig(mod3)
        sig4 <- sig(mod4)
        for (i in 1:k){
            cat(varnames[i], "&")
            cat(paste(mrkup[sig1[i]],signif(mod1$par[i],3),"} & ", mrkup[sig2[i]],signif(mod2$par[i],3),"} & ",mrkup[sig3[i]],signif(mod3$par[i],3),"} & ", mrkup[sig4[i]],signif(mod4$par[i],3),"} \\\\ \n", sep=""))
            cat(paste("&\\footnotesize{(",signif(mod1$se[i],3),")} &\\footnotesize{(",signif(mod2$se[i],3),")} & \\footnotesize{(",signif(mod3$se[i],3),")} &\\footnotesize{(",signif(mod4$se[i],3),")} \\\\ \n", sep=""))
        }
    }
    if (n==5){
        k <- max(length(mod1$par), length(mod2$par), length(mod3$par), length(mod4$par), length(mod5$par))
        sig1 <- sig(mod1)
        sig2 <- sig(mod2)
        sig3 <- sig(mod3)
        sig4 <- sig(mod4)
        sig5 <- sig(mod5)
        for (i in 1:k){
            cat(varnames[i], "&")
            cat(paste(mrkup[sig1[i]],signif(mod1$par[i],3),"} & (",signif(mod1$se[i],3),") &", sep=""))
            cat(paste(mrkup[sig2[i]],signif(mod2$par[i],3),"} & (",signif(mod2$se[i],3),") &", sep=""))
            cat(paste(mrkup[sig3[i]],signif(mod3$par[i],3),"} & (",signif(mod3$se[i],3),") &", sep=""))
            cat(paste(mrkup[sig4[i]],signif(mod4$par[i],3),"} & (",signif(mod4$se[i],3),") &", sep=""))
            cat(paste(mrkup[sig5[i]],signif(mod5$par[i],3),"} & (",signif(mod5$se[i],3),")", sep=""), "\\\\ \n")
        }
    }
}
   
pvals <- function(n,  mod1, mod2=c(), mod3=c(), mod4=c(), varnames){    
    if (n>1) {
        cat("# of models not implemented yet \n\n")
        break
    }
    mod <- mod1
    parameter<-as.matrix(mod$par)
    vacov<-as.matrix(solve(-1*mod1$hessian, tol=1e-24))
    stderror<-as.matrix(sqrt(diag(vacov)))
    zstat<-as.matrix(parameter / stderror)
    pvalues <- round(2*pnorm(abs(zstat),lower.tail=FALSE),4)
    
     
    cat("results in order:", varnames,"\n")
    cat("parameter estimates: ", mod$par, "\n")
    cat("standard error: ", stderror, "\n")
    cat("z-statistics: ", zstat, "\n")
    cat("p-values: ", pvalues, "\n")
}

getp <- function(mod){
    parameter<-as.matrix(mod$par)
    zstat<-as.matrix(parameter / mod$se)
    pvalues <- round(2*pnorm(abs(zstat),lower.tail=FALSE),4)
    return(pvalues)
}
